home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / uucp / devices.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-30  |  4.3 KB  |  206 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "../misc/misc.h"
  5. #include "internal.h"
  6. #include "uucp.h"
  7. #include "uucp.m"
  8. #include "../paths.h"
  9. #include "../netconf/netconf.h"
  10.  
  11. static UUCP_HELP_FILE help_devices ("devices");
  12. static CONFIG_FILE f_devices (VAR_LIB_UUCP_DEVICES
  13.     ,help_devices
  14.     ,CONFIGF_OPTIONNAL|CONFIGF_MANAGED
  15.     ,"uucp","uucp",0660);
  16.  
  17.  
  18. PUBLIC DEVICE::DEVICE ()
  19. {
  20.     speed = 38400;
  21. }
  22.  
  23. static const char *device_copycond (
  24.     SSTRING &dst,
  25.     const char *buf)
  26. {
  27.     buf = dst.copyword (buf);
  28.     if (dst.cmp("-")==0) dst.setfrom ("");
  29.     return buf;
  30. }
  31.  
  32. PUBLIC DEVICE::DEVICE (
  33.     const char *buf,    // Buffer to parse from the Permissions file
  34.     const SSTRING &_comments,    // Comments preceding the definition
  35.     char *err)            // Will contain error message or '\0'
  36. {
  37.     comments.setfrom (_comments);
  38.     comments.strip_end();
  39.     err[0] = '\0';
  40.     buf = device_copycond (acu,buf);
  41.     buf = device_copycond (device,buf);
  42.     buf = device_copycond (device2,buf);
  43.     char word[100];
  44.     buf = str_copyword (word,buf);
  45.     speed = atoi(word);
  46.     buf = device_copycond (type,buf);
  47.     options.copyword (buf);
  48. }
  49.  
  50. static void device_writecond(SSTRING &s, FILE *fout)
  51. {
  52.     const char *str = s.get();
  53.     if (str[0] == '\0') str = "-";
  54.     fprintf (fout,"%s ",str);
  55. }
  56. /*
  57.     Write one record in the Permissions file
  58. */
  59. PUBLIC void DEVICE::write (FILE *fout)
  60. {
  61.     comment_write (comments,fout);
  62.     device_writecond (acu,fout);
  63.     device_writecond (device,fout);
  64.     device_writecond (device2,fout);
  65.     fprintf (fout,"%d ",speed);
  66.     device_writecond (type,fout);
  67.     fprintf (fout,"%s\n",options.get());
  68. }
  69.  
  70. /*
  71.     Return -1 if escape
  72.     Return  0 if accept the changes
  73.     Return  1 if the record must be deleted
  74. */
  75. PUBLIC int DEVICE::edit (const DIALERS &dial)
  76. {
  77.     int ret = -1;
  78.     DIALOG dia;
  79.     FIELD_COMBO *comb = dia.newf_combo (MSG_R(F_TYPE),acu);
  80.     comb->addopt ("ACU",MSG_R(M_MODEM));
  81.     comb->addopt ("Direct",MSG_R(M_DIRECT));
  82.     comb->addopt ("Tcp",MSG_R(M_UUCPTCP));
  83.     serial_setfield (device,dia);
  84.     SSTRING baudstr;
  85.     baud_setfield (speed,baudstr,dia);
  86.     comb = dia.newf_combo (MSG_U(F_MODEMTYPE,"Modem type"),type);
  87.     for (int i=0; i<dial.getnb(); i++){
  88.         DIALER *d = dial.getitem(i);
  89.         comb->addopt (d->type.get());
  90.     }
  91.     int nof = 0;
  92.     while (1){
  93.         MENU_STATUS code = dia.edit (
  94.             MSG_U(T_DEVICE,"Uucp device configuration")
  95.             ,MSG_U(I_DEVICE,"You must specify the speed range\n"
  96.                 "and the modem type associated with a serial\n"
  97.                 "device.")
  98.             ,help_devices
  99.             ,nof
  100.             ,MENUBUT_CANCEL|MENUBUT_DEL|MENUBUT_ACCEPT);
  101.         if (code == MENU_CANCEL || code == MENU_ESCAPE){
  102.             break;
  103.         }else if (code == MENU_DEL){
  104.             ret = 1;
  105.             break;
  106.         }else if (code == MENU_ACCEPT){
  107.             ret = 0;
  108.             break;
  109.         }
  110.     }
  111.     if (ret != 0) dia.restore();
  112.     return ret;
  113. }
  114.  
  115.  
  116. PUBLIC DEVICES::DEVICES()
  117.     : CONFIG_OBJS (f_devices)
  118. {
  119. }
  120.  
  121. PUBLIC DEVICE *DEVICES::getitem(int no)
  122. {
  123.     return (DEVICE*)ARRAY::getitem(no);
  124. }
  125.  
  126. PROTECTED CONFIG_OBJ *DEVICES::newobj (
  127.     const char *buf,    // Buffer to parse from the Permissions file
  128.     const SSTRING &_comments,    // Comments preceding the definition
  129.     char *err)            // Will contain error message or '\0'
  130. {
  131.     return new DEVICE (buf,_comments,err);
  132. }
  133.  
  134. static int cmp_by_name (const ARRAY_OBJ *o1, const ARRAY_OBJ *o2)
  135. {
  136.     DEVICE *s1 = (DEVICE*)o1;
  137.     DEVICE *s2 = (DEVICE*)o2;
  138.     int ret = s1->acu.cmp(s2->acu);
  139.     if (ret == 0){
  140.         ret = s1->device.cmp(s2->device);
  141.     }
  142.     return ret;
  143. }
  144.  
  145. PUBLIC void DEVICES::sort()
  146. {
  147.     ARRAY::sort (cmp_by_name);
  148. }
  149.  
  150.  
  151. PUBLIC void DEVICES::edit()
  152. {
  153.     DIALERS dial;
  154.     dial.read();
  155.     while (1){
  156.         DIALOG dia;
  157.         int n = getnb();
  158.         sort();
  159.         for (int i=0; i<n; i++){
  160.             DEVICE *s = getitem(i);
  161.             char buf[100];
  162.             sprintf (buf,"%-15s %s",s->device.get(),s->type.get());
  163.             dia.new_menuitem (s->acu.get(),buf);
  164.         }
  165.         dia.addwhat (MSG_R(I_ADDSYS));
  166.         int sel;
  167.         MENU_STATUS code = dia.editmenu (
  168.             MSG_U(T_DEVICES,"Edit uucp devices configuration")
  169.             ,MSG_U(I_DEVICES
  170.              ,"Each configuration control an uucp device\n"
  171.               "and the way this device (modem) is\n"
  172.               "initialised/dialed\n")
  173.             ,help_devices
  174.             ,sel,0);
  175.         if (code == MENU_OK){
  176.             if (n > 0){
  177.                 DEVICE *s = getitem(sel);
  178.                 int ok = s->edit(dial);
  179.                 if (ok >= 0){
  180.                     if (ok == 1) remove_del(s);
  181.                     write();
  182.                 }
  183.             }
  184.         }else if (code == MENU_ESCAPE || code == MENU_QUIT){
  185.             break;
  186.         }else if (code == MENU_ADD){
  187.             DEVICE *s = new DEVICE;
  188.             if (s->edit(dial)==0){
  189.                 add (s);
  190.                 write();
  191.             }else{
  192.                 delete s;
  193.             }
  194.         }
  195.     }
  196. }
  197.  
  198. void devices_edit()
  199. {
  200.     DEVICES dev;
  201.     dev.read();
  202.     dev.edit();
  203. }
  204.  
  205.  
  206.